<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Order_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/order_entity', 'Order_Entity');
    }

    public function countListShopSumary($option=array()){
        $select = "a.shop_id";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_user_name . ' b', 'c.user_id=b.id');
        if (count($option) > 0) {
            $this->db->where($option);
        } else {
            $this->db->where('a.deleted_at', NULL);
            $this->db->where('b.deleted_at', NULL);
            $this->db->where('c.deleted_at', NULL);
            $this->db->where('b.active_flg', 1);
            $this->db->where('c.active_flg', 1);
        }
        $this->db->group_by('a.shop_id');
        $result = $this->db->get()->result();
        return count($result);
    }

    public function listShopSumary($limit=ORDER_LIMIT, $offset=0, $option=array()){
        $select = "count(a.id) as order_count, c.title as shop_title, c.slug as shop_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_user_name . ' b', 'c.user_id=b.id');
        if (count($option) > 0) {
            $this->db->where($option);
        } else {
            $this->db->where('a.deleted_at', NULL);
            $this->db->where('b.deleted_at', NULL);
            $this->db->where('c.deleted_at', NULL);
            $this->db->where('b.active_flg', 1);
            $this->db->where('c.active_flg', 1);
        }
        $this->db->group_by('a.shop_id');
        $this->db->limit($limit, $offset);
        $this->db->order_by('order_count', 'DESC');
        $result = $this->db->get()->result('Order_Entity');
        return $result;
    }

    public function countOrderPerShop($shop_slug, $option = array())
    {
        $this->db->select('a.id', true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_user_name . ' b', 'a.user_id=b.id', 'left');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->db->where('c.slug', $shop_slug);
        return $this->db->count_all_results();
    }

    public function paging($shop_slug, $limit=ORDER_LIMIT, $offset=0, $option = array())
    {
        $select = "a.id, a.shop_id, a.name, a.email, a.phone, a.address, a.receiver_name, a.receiver_email, a.receiver_phone, a.receiver_address, a.note, a.created_at, a.deleted_at, b.image_folder as shop_image_folder, c.title as shop_title, c.slug as shop_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_user_name . ' b', 'c.user_id=b.id', 'left');
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->db->where('c.slug', $shop_slug);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Order_Entity');
        return $result;
    }

    public function billing($aShop, $options, $cart) {
        $date = new DateTime();
        $createdAt = $date->format('Y-m-d H:i:s');
        $this->db->trans_begin();
        $options['created_at'] = $createdAt;
        foreach ($aShop as $shop_id => $shop) {
            $order = array(
                'shop_id'          => $shop_id,
                'name'             => $options['name'],
                'email'            => $options['email'],
                'phone'            => $options['phone'],
                'address'          => $options['address'],
                'receiver_name'    => $options['receiver_name'],
                'receiver_email'   => $options['receiver_email'],
                'receiver_phone'   => $options['receiver_phone'],
                'receiver_address' => $options['receiver_address'],
                'note'             => $options['note'],
                'created_at'       => $options['created_at']
            );
            if(isset($options['user_id'])){
                $order['user_id'] = $options['user_id'];
            }
            $this->db->insert($this->table_order_name, $order);
            $orderId = $this->db->insert_id();
            foreach ($shop as $product) {
                $orderDetail = array(
                    'order_id'     => $orderId,
                    'product_id'   => $product['id'],
                    'product_code' => $product['options']['code'],
                    'price'        => $product['price'],
                    'count'        => $product['qty'],
                    );
                $this->db->insert($this->table_order_detail_name, $orderDetail);
            }
        }

        //send email to user
        $data['options'] = $options;
        $data['cart']    = $cart;
        $aInfo = array(
            'subject'  => SITE_NAME . ' - Xác nhận đặt hàng',
            'template' => 'mail-template/billing.html.php',
            'data'     => $data,
            'email'    => $options['email'],
            'address'  => array()
        );
        foreach($cart as $index=>$item){
            $aInfo['attachment'][$index] = get_attachment(
                $item['options']['shop_image_folder']. '/' . PRODUCT_IMAGE_URL . thumb_name($item['options']['image'])
            );
        }

        $this->load->library('event');
        $resultUser = $this->event->sendMail($aInfo);

        //send email to shop
        $aOrderInfo['subject']    = SITE_NAME . ' - Thông báo đặt hàng';
        $aOrderInfo['template']   = 'mail-template/user-shopping.html.php';
        $aOrderInfo['address']    = array();
        foreach($aShop as $shop_id => $shop){
            $aOrderInfo['attachment'] = array();
            $aOrderInfo['data']['cart']    = $shop;
            $aOrderInfo['data']['options'] = $options;
            foreach($shop as $index=>$product){
                $aOrderInfo['email'] = $product['options']['email'];
                $aOrderInfo['attachment'][$index] = get_attachment(
                    $product['options']['shop_image_folder']. '/' . PRODUCT_IMAGE_URL . thumb_name($product['options']['image'])
                );
            }
            $this->event->sendMail($aOrderInfo);
        }
        if ($this->db->trans_status() === false || !$resultUser) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function get($id){
        $this->load->model('entity/order_detail_entity', 'Order_Detail_Entity');
        $select = "a.id, a.product_id, a.product_code, a.price, a.count";
        $select .= ", c.title as product_title, c.slug as product_slug, b.image as product_image";
        $this->db->select($select, true);
        $this->db->from($this->table_order_detail_name . ' a');
        $this->db->join($this->table_product_details_name . ' b', 'a.product_id=b.product_id and a.product_code=b.code');
        $this->db->join($this->table_product_name . ' c', 'c.id=b.product_id');
        $this->db->where('a.order_id', $id);
        $result = $this->db->get()->result('Order_Detail_Entity');
        return $result;
    }

    public function countSearch($search_key, $option=array()){
        $select = "a.shop_id";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_user_name . ' b', 'c.user_id=b.id');
        if (count($option) > 0) {
            $this->db->where($option);
        } else {
            $this->db->where('a.deleted_at', NULL);
            $this->db->where('b.deleted_at', NULL);
            $this->db->where('c.deleted_at', NULL);
            $this->db->where('b.active_flg', 1);
            $this->db->where('c.active_flg', 1);
        }
        $this->db->like('c.title', $search_key);
        $this->db->group_by('a.shop_id');
        $result = $this->db->get()->result();
        return count($result);
    }

    public function search($search_key, $limit=ORDER_LIMIT, $offset=0, $option=array()){
        $select = "count(a.id) as order_count, c.title as shop_title, c.slug as shop_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_order_name . ' a');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_user_name . ' b', 'c.user_id=b.id');
        if (count($option) > 0) {
            $this->db->where($option);
        } else {
            $this->db->where('a.deleted_at', NULL);
            $this->db->where('b.deleted_at', NULL);
            $this->db->where('c.deleted_at', NULL);
            $this->db->where('b.active_flg', 1);
            $this->db->where('c.active_flg', 1);
        }
        $this->db->like('c.title', $search_key);
        $this->db->group_by('a.shop_id');
        $this->db->limit($limit, $offset);
        $this->db->order_by('order_count', 'DESC');
        $result = $this->db->get()->result('Order_Entity');
        return $result;
    }
}
?>